package tt.dz.appserver.dao.ext;

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import tt.dz.appserver.dao.base.BasisDao;
import tt.dz.appserver.entity.model.pub.ThumbUp;

@Repository
public class ThumbUpDao extends BasisDao<ThumbUp>{

	
	/**
	 * 获取点赞信息，通过关联类型和关联ID
	 * @return
	 */
	@Cacheable(value="thumbUps",key="'associatedType'+#associatedType+',associatedId'+#associatedId")
	public List<ThumbUp> findThumbUpByAssociatedTypeAndAssociatedId(Integer associatedType,String associatedId){
		
		ThumbUp thumbUp = new ThumbUp();
		thumbUp.setAssociated_type(associatedType);
		thumbUp.setAssociated_id(associatedId);
		return super.findExample(thumbUp, null, null, "create_time", 1);
	}
	
	/**
	 * 获取单个点赞信息，通过用户Id,关联类型和关联ID
	 * @return
	 */
	@Cacheable(value="thumbUp",key="'userId'+#userId+',associatedType'+#associatedType+',associatedId'+#associatedId")
	public ThumbUp findThumbUpByUserIdAndAssociatedTypeAndAssociatedId(String userId, Integer associatedType,
			String associatedId){
		
		ThumbUp thumbUp = new ThumbUp();
		thumbUp.setAssociated_type(associatedType);
		thumbUp.setAssociated_id(associatedId);
		thumbUp.setUser_id(userId);
		List<ThumbUp> thumbUps = super.findExample(thumbUp);
		if(thumbUps.size()>0){
			return thumbUps.get(0);
		}
		return null;
	}

	@Caching(evict={
		@CacheEvict(value="thumbUp",key="'userId'+#obj.user_id+',associatedType'+#obj.associated_type+',associatedId'+#obj.associated_id"),
		@CacheEvict(value="thumbUps",key="'associatedType'+#obj.associated_type+',associatedId'+#obj.associated_id")
	})
	public int save(ThumbUp obj) {
		return super.save(obj);
	}

	@Caching(evict={
			@CacheEvict(value="thumbUp",key="'userId'+#obj.user_id+',associatedType'+#obj.associated_type+',associatedId'+#obj.associated_id"),
		@CacheEvict(value="thumbUps",key="'associatedType'+#obj.associated_type+',associatedId'+#obj.associated_id")
	})
	public int delete(ThumbUp obj) {
		return super.delete(obj);
	}
}
